
var sql = require("../sql/sql");
var userSQL = require('../../config/Usersql');

class Forum extends sql{
	constructor(){
        super();

        this.data = {
            hotart:4,
            page:0,
            num:10
        }
    }


	hotart(type,num){
        num = num==null?this.data.hotart:parseInt(num);
        
		return new Promise((resolve,reject)=>{
			this.query_sql(userSQL.forum.queryHotart,[type,num]).then(
				message=>{
					resolve(message);
				},error=>{
					reject(error);
				}
			);
		});
	}
	
    addArticle(id,author,title,type,desc,content,click,reply){
        if(id==null || desc==null||click==null||reply==null){
            desc=null;
            var date = new Date().getTime().toString();
            console.log(date);
            id = this.MD5(date);
            console.log(id);
            click=0;
            reply=0;
        }else{
            desc=desc;
            click=click;
            reply=reply;
        }
        return new Promise((resolve,reject)=>{
            this.insert_sql(userSQL.home.insertArticle,[id,author,title,type,desc,content,parseInt(click),parseInt(reply)]).then(
                message=>{
                    resolve(message);
                },error=>{
                    reject(error);
                }
            )
        });
	}
	
	getArticleCount(type){
		return new Promise((resolve,reject)=>{
			this.query_sql(userSQL.forum.queryArticleCount,[type]).then(
				message=>{
					
					//console.log();
					resolve(message);
					
				},error=>{
					reject(error);
				}
			)
		});
	}

	article(type,page,num){
        page = page==null?this.data.page:parseInt(page);
        num = num==null?this.data.num:parseInt(num);

		console.log([type,parseInt(num)]);
		return new Promise((resolve,reject)=>{
			this.query_sql(userSQL.forum.queryArticle,[type,page*num,num]).then(
				message=>{
					resolve(message);
/*					var result = message;
					this.query_sql(userSQL.forum.queryArticleCount,[type]).then(
						message=>{
							result["count"] = message[0]["COUNT(type)"];
							//console.log();
							resolve(result);
							
						},error=>{
							reject(error);
						}
					)*/
				},error=>{
					reject(error);
				}
			);
		});
	}


	artinfo(id){
		//console.log([id]);
		return new Promise((resolve,reject)=>{
			this.query_sql(userSQL.forum.queryArtInfo,[id]).then(
				message=>{
					resolve(message);
				},error=>{
					reject(error);
				}
			);
		});
	}

	showreply(article_id){
        
		return new Promise((resolve,reject)=>{
			this.query_sql(userSQL.forum.queryReply,[article_id]).then(
				message=>{
					resolve(message);
				},error=>{
					reject(error);
				}
			);
		});
	}

}
module.exports = new Forum();
